# coding:utf-8

"""
tf_demo10_tensorboard
Created on 2016/12/9 16:07
@author: GuoYufu
@group : OceanHorn
@contact: OceanHorn@163.com
"""


from tf_demo06_define_addLayer_for_demo10 import *
import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":

    x_data = np.linspace(start=-1, stop=1, num=300)[:, np.newaxis]
    noise = np.random.normal(loc=0, scale=0.05, size=x_data.shape)

    y_data = np.square(x_data) - 0.5 + noise

    with tf.name_scope('inputs'):
        xs = tf.placeholder(dtype=tf.float32, shape=[None, 1], name='x_input')
        ys = tf.placeholder(dtype=tf.float32, shape=[None, 1], name='y_input')

    # add hidder layer
    with tf.name_scope('layer_one'):
        layer1_outputs = add_layer(inputs=xs, in_size=1, out_size=10, activation_function=tf.nn.relu)

    # add output layer
    with tf.name_scope('layer_output'):
        prediction_layer_outputs = add_layer(inputs=layer1_outputs, in_size=10, out_size=1, activation_function=None)

    with tf.name_scope('loss'):
        loss = tf.reduce_mean(
            tf.reduce_sum(
                tf.square(ys - prediction_layer_outputs), reduction_indices=[1]
            )
        )
    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(learning_rate=0.2).minimize(loss)

    initialize = tf.global_variables_initializer()

    # 显示图像
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.scatter(x_data, y_data)
    plt.ion() # 激活动态
    plt.show()



    with tf.Session() as session:

        writer = tf.train.SummaryWriter("demo10/", session.graph)

        session.run(initialize)

        for i in range(2000):
            session.run(train_step, feed_dict={xs: x_data, ys:y_data})

            if i % 50 == 0:
            #     print session.run(loss, feed_dict={xs: x_data, ys:y_data})

                prediction_value = session.run(fetches=prediction_layer_outputs, feed_dict={xs: x_data, ys:y_data})

                try:
                    ax.lines.remove(lines[0])
                except Exception:
                    pass
                lines = ax.plot(x_data, prediction_value, 'g-', lw=4)
                plt.pause(0.1)